package com.jeespring.rocketmq.delay;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @description SyncProducer
 * @author 张朝阳
 * @date 2021/1/10 22:08
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("group1");

        producer.setNamesrvAddr("192.168.19.10:9876");
        producer.start();

        for (int i = 0; i < 10; i++) {
            //public Message(String topic, String tags, byte[] body)
            String msg = "延迟消息" + i;
            Message message = new Message("delay-message","sync",msg.getBytes(RemotingHelper.DEFAULT_CHARSET));

            //messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
            message.setDelayTimeLevel(3);

            SendResult sendResult = producer.send(message);

            System.out.println(JSON.toJSONString(sendResult));

        }

        producer.shutdown();
    }
}
